package com.czz.manage.controller.teacher;

import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Period;
import com.czz.manage.service.PeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: PeriodTeacherController
 * @Author Czz
 * @Date 2020/1/22
 * @Time 17:23
 **/
@RestController
@RequestMapping(value = "/teacher/periods")
public class PeriodTeacherController {

    @Autowired
    PeriodService periodService;

    /**
    * @Description 新增课时
    * @Param [period]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/27
    * @Time 14:58
    **/
    @PostMapping(value = "")
    public R savePeriod(Period period){
        if (ObjectUtils.isEmpty(period)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        periodService.save(period);
        return R.ok();
    }

    /**
    * @Description 根据id获取课时信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/27
    * @Time 14:58
    **/
    @GetMapping(value = "/{id}")
    public R getPeriodById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Period period = periodService.getById(id);
        return R.ok().data("item", period);
    }

    /**
    * @Description 根据id更新课时信息
    * @Param [id, period]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/27
    * @Time 15:03
    **/
    @PutMapping(value = "/{id}")
    public R updatePeriodById(@PathVariable String id, Period period){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(period)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        period.setPeriodId(id);
        periodService.updatePeriodById(period);
        return R.ok();
    }

    /**
    * @Description 根据id删除课时
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/27
    * @Time 15:03
    **/
    @DeleteMapping(value = "/{id}")
    public R deletePeriodById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        periodService.deletePeriodById(id);
        return R.ok();
    }
}
